package com.maven.zh.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.maven.zh.bean.Datas;
import com.maven.zh.bean.Manager;
import com.maven.zh.bean.Page;
import com.maven.zh.bean.Role;
import com.maven.zh.service.RoleService;
import com.maven.zh.util.Const;



@Controller
@RequestMapping("/role")
public class RoleController extends BaseController{

	@Autowired
	private RoleService roleService;
	
	/**
	 * 跳转到角色列表页面
	 * @return
	 */
	@RequestMapping("/list")
	public String list(HttpSession session) {
		
		//查询登录的用户信息
		Manager loginManager = (Manager)session.getAttribute(Const.SESSION_MANAGER);
		if(loginManager==null){
			return "login";
		}else{
			return "role/list";
		}
	}
	
	/**
	 * 跳转到增加角色信息页面
	 * @return
	 */
	@RequestMapping("/add")
	public String add() {
		return "role/add";
	}
	
	/**
	 * 跳转到分配角色的页面
	 * @return
	 */
	@RequestMapping("/assign")
	public String assign() {
		return "role/assign";
	}
	
	/**
	 * 跳转到修改角色信息的页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public String edit( @PathVariable("id")Integer id, Model model ) {
		
		Role role = roleService.queryRoleById(id);
		model.addAttribute("role", role);
		
		return "role/edit";
	}
	
	/**
	 * 增加角色信息的方法
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/insert")
	public Object insert( Role role ) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			roleService.insertRole(role);
			resultMap.put("success", true);
		} catch ( Exception e ) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		
		return resultMap;
	}
	
	/**
	 * 修改角色信息的方法
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Object update( Role role ) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			roleService.updateRole(role);
			resultMap.put("success", true);
		} catch ( Exception e ) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		
		return resultMap;
	}
	
	/**
	 * 删除角色信息的方法
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Object delete( Integer id ) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			roleService.deleteRoleById(id);
			resultMap.put("success", true);
		} catch ( Exception e ) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		
		return resultMap;
	}
	
	/**
	 * 批量删除角色信息的方法
	 * @param ds
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deletes")
	public Object deletes( Datas ds ) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			roleService.deleteRoles(ds);
			resultMap.put("success", true);
		} catch ( Exception e ) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		
		return resultMap;
	}
	
	/**
	 *分配角色权限方法
	 * @param roleid
	 * @param ds
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/doassign")
	public Object doassign( Integer roleid, Datas ds ) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			roleService.doassign(roleid, ds);
			resultMap.put("success", true);
		} catch ( Exception e ) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		
		return resultMap;
	}
	
	/**
	 * 分页查询角色信息
	 * @param pageno
	 * @param pagesize
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryPageRoles")
	public Object queryPageRoles( Integer pageno, Integer pagesize ) {
		Map<String, Object> resultMap = new HashMap<String, Object>();// []
		resultMap.put("success", true);
		
		Page<Role> dataPage = roleService.queryPage(pageno, pagesize);
		dataPage.setPageno(pageno);
		dataPage.setPagesize(pagesize);
		resultMap.put("datapage", dataPage);
		
		return resultMap;
	}
	
	

}
